using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using THSLib;using THSERP.BaseProvider;
using System.Drawing;

namespace THSERP.Modules.CreditNote
{
    public class CreditNoteModule : BaseModuleERP
    {
        #region Constant for CreditNote
        #region CreditNoteType Constants
        public const string cstCreditNoteTypeGiftVoucher = "GiftVoucher";
        public const string cstCreditNoteTypeCreditNote = "CreditNote";
        #endregion

        #region CreditNoteStatus Constants
        public const string cstCreditNoteStatusNew = "New";
        public const string cstCreditNoteStatusPost = "Post";
        #endregion

        public const String cstCreditNoteConfigValidDays = "CreditNoteValidDays";
        #endregion

        private guiCustomersInfo CustomerInfoForm = null;

        public CreditNoteModule()
		{
			Name="CreditNote";
			CurrentModuleEntity= new CreditNoteEntities();
			CurrentModuleEntity.Module=this;
			InitializeModule();

            THSLookupEdit lke = (THSLookupEdit)Controls["fld_lkeFK_ARCustomerID"];
            lke.CloseUp += new DevExpress.XtraEditors.Controls.CloseUpEventHandler(ChangeCustomer);

            //Disable lookupedit if user is an employee
            ARCreditNotesInfo objCreditNotesInfo = (ARCreditNotesInfo)CurrentModuleEntity.MainObject;
            if (THSApp.CurrentUsersInfo.FK_HREmployeeID > 0)
            {
                objCreditNotesInfo.FK_HREmployeeID = THSApp.CurrentUsersInfo.FK_HREmployeeID;
                foreach (Control ctrl in Controls.Values)
                    if (!String.IsNullOrEmpty(ctrl.AccessibleName) && ctrl.AccessibleName.Contains("FK_HREmployeeID"))
                    {
                        (ctrl as THSLookupEdit).EditValue = THSApp.CurrentUsersInfo.FK_HREmployeeID;
                        ctrl.Enabled = false;
                    }

                //Show picture
                HREmployeesInfo objHREmployeesInfo = (HREmployeesInfo)new HREmployeesController().GetObjectByID(Convert.ToInt32(THSApp.CurrentUsersInfo.FK_HREmployeeID));
                if (objHREmployeesInfo != null)
                {
                    objCreditNotesInfo.ARCreditNoteEmployeePicture = objHREmployeesInfo.HREmployeePicture;
                    CurrentModuleEntity.UpdateMainObjectBindingSource();
                }
            }

            DisplayLabelText(objCreditNotesInfo);
		}

        public override void ActionNew()
        {
            base.ActionNew();
            //Disable lookupedit if user is an employee
            ARCreditNotesInfo objCreditNotesInfo = (ARCreditNotesInfo)CurrentModuleEntity.MainObject;

            if (THSApp.CurrentUsersInfo.FK_HREmployeeID > 0)
            {
                objCreditNotesInfo.FK_HREmployeeID = THSApp.CurrentUsersInfo.FK_HREmployeeID;
                //Show employee picture
                HREmployeesInfo objHREmployeesInfo = (HREmployeesInfo)new HREmployeesController().GetObjectByID(objCreditNotesInfo.FK_HREmployeeID);
                if (objHREmployeesInfo != null)
                {
                    ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).ARCreditNoteEmployeePicture = objHREmployeesInfo.HREmployeePicture;
                    CurrentModuleEntity.UpdateMainObjectBindingSource();
                }
            }

            //Set Default CreditNote Type is Manual
            ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).ARCreditNoteTypeCombo = CreditNoteModule.cstCreditNoteTypeGiftVoucher;

            ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).FK_BRBranchID = THSApp.CurrentCompanyInfo.FK_BRBranchID;
            ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).FK_ICStockID = THSApp.CurrentCompanyInfo.FK_ICStockID;
            ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).ARCreditNoteDate = DateTime.Now;
            CurrentModuleEntity.UpdateMainObjectBindingSource();
            DisplayLabelText(objCreditNotesInfo);
        }

        /// <summary>
        /// Set values of properties when select a customer. 
        /// </summary>
        /// <remarks>Hieu.nguyen_29/3/2010</remarks>
        /// 
        public override void SetDefaultValuesFromCustomer()
        {
            
            CreditNoteEntities entity = (CreditNoteEntities)CurrentModuleEntity;

            int customerID = Convert.ToInt32(THSDbUtil.GetPropertyValue(entity.MainObject, "FK_ARCustomerID"));
            ARCustomersController objCustomersController = new ARCustomersController();
            ARCustomersInfo objCustomersInfo = (ARCustomersInfo)objCustomersController.GetObjectByID(customerID);
            if (objCustomersInfo != null)
            {
                //Set total amount, discount, price level for customer_Hieu.nguyen_30/3/2010
                ARPriceLevelsController objPriceLevelsController = new ARPriceLevelsController();
                ARPriceLevelsInfo objPriceLevelsInfo = (ARPriceLevelsInfo)objPriceLevelsController.GetObjectByID(objCustomersInfo.FK_ARPriceLevelID);
                objCustomersInfo.ARCustomerPriceLevelName = objPriceLevelsInfo.ARPriceLevelName;
                objCustomersInfo.ARCustomerTotalPurchase = objCustomersController.GetTotalInvoiceAmount(objCustomersInfo.ARCustomerID);
                objCustomersInfo.ARCustomerTotalDiscount = objCustomersController.GetTotalInvoiceDiscount(objCustomersInfo.ARCustomerID);
                objCustomersInfo.ARCustomerTotalRefund = objCustomersController.GetTotalInvoiceRefund(objCustomersInfo.ARCustomerID);

                ShowCustomerInfo(objCustomersInfo, false);
            }
        }

        public void ShowCustomerInfo(ARCustomersInfo objCustomersInfo, bool showPriceLevelPanel)
        {
            if (CustomerInfoForm == null)
            {
                CustomerInfoForm = new guiCustomersInfo();
                CustomerInfoForm.Module = this;
            }
            CustomerInfoForm.DisplayCustomerInfo(objCustomersInfo);
            CustomerInfoForm.fld_chkApplyPriceLevel.Checked = false;
            CustomerInfoForm.fld_pnlPriceLevel.Visible = showPriceLevelPanel;
            CustomerInfoForm.ShowDialog();
        }

        public void ChangeCustomer(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
        {
            if (!Toolbar.IsNullOrNoneAction())
            {
                THSLookupEdit lke = (THSLookupEdit)sender;
                if (e.Value != lke.OldEditValue)
                {
                    CreditNoteEntities entity = (CreditNoteEntities)CurrentModuleEntity;
                    ((ARCreditNotesInfo)entity.MainObject).FK_ARCustomerID = Convert.ToInt32(e.Value);
                    SetDefaultValuesFromCustomer();
                }
            }
        }

        public override void Invalidate(int iObjectID)
        {
            base.Invalidate(iObjectID);

            CreditNoteEntities entity = (CreditNoteEntities)CurrentModuleEntity;
            ARCreditNotesInfo objCreditNotesInfo = (ARCreditNotesInfo)entity.MainObject;
            //set picture
            HREmployeesInfo objHREmployeesInfo = (HREmployeesInfo)new HREmployeesController().GetObjectByID(objCreditNotesInfo.FK_HREmployeeID);
            if (objHREmployeesInfo != null)
            {
                ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).ARCreditNoteEmployeePicture = objHREmployeesInfo.HREmployeePicture;
                CurrentModuleEntity.UpdateMainObjectBindingSource();
            }
            DisplayLabelText(objCreditNotesInfo);
        }

        /// <summary>
        /// validated staff
        /// </summary>
        ///<remarks>HieuNH_15/04/2010</remarks>
        ///
        public void Staff_Validated(object sender, EventArgs e)
        {
            THSLookupEdit lke = (THSLookupEdit)sender;
            if (lke != null && lke.EditValue != lke.OldEditValue)
            {
                HREmployeesInfo objHREmployeesInfo = (HREmployeesInfo)new HREmployeesController().GetObjectByID(Convert.ToInt32(lke.EditValue));
                if (objHREmployeesInfo != null)
                {
                    ((ARCreditNotesInfo)CurrentModuleEntity.MainObject).ARCreditNoteEmployeePicture = objHREmployeesInfo.HREmployeePicture;
                    CurrentModuleEntity.UpdateMainObjectBindingSource();
                }
            }
        }

        public void EditCustomer()
        {
            ARCreditNotesInfo objCreditNotesInfo = (ARCreditNotesInfo)CurrentModuleEntity.MainObject;
            Modules.Customer.CustomerModule customerModule = (Modules.Customer.CustomerModule)ParentScreen.ShowModule("Customer");
            for (int i = 0; i < customerModule.Toolbar.ObjectCollection.Tables[0].Rows.Count; i++)
                if (objCreditNotesInfo.FK_ARCustomerID == Convert.ToInt32(customerModule.Toolbar.ObjectCollection.Tables[0].Rows[i]["ARCustomerID"]))
                {
                    customerModule.Toolbar.CurrentIndex = i;
                    customerModule.FocusRowOfGridSearchResultByToolbarCurrentIndex();
                    customerModule.Invalidate(objCreditNotesInfo.FK_ARCustomerID);
                    customerModule.ActionEdit();
                    return;
                }
        }
    }
}
